package com.chuang.bootplus.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuang.bootplus.base.enumerate.HttpStatusEnum;
import com.chuang.bootplus.base.exception.BusException;
import com.chuang.bootplus.base.utils.ApiResponse;
import com.chuang.bootplus.base.utils.BeanUtil;
import com.chuang.bootplus.base.utils.StringUtil;
import com.chuang.bootplus.entity.User;
import com.chuang.bootplus.mapper.UserMapper;

import com.chuang.bootplus.po.user.*;
import com.chuang.bootplus.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.chuang.bootplus.vo.user.UserInfoVO;
import com.chuang.bootplus.vo.user.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-08-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private JavaMailSender mailSender;//一定要用@Autowired



    @Override
    public ApiResponse<Void> deleteUser(UserDeletePO po) {
        if(getById(po.getEmpId()).getRoleId()==10){
            throw new BusException("Sorry,您不具备该权限");
        }
        removeById(po.getId());
        return new ApiResponse("删除成功");
    }

    @Override
    public ApiResponse<Void> addUser(UserPO po) {
        if(getById(po.getEmpId()).getRoleId()==10){
            throw new BusException("Sorry,您不具备该权限");
        }
        User user = BeanUtil.beanA2beanB(po, User.class);
        save(user);
        return new ApiResponse("添加成功");
    }

    @Override
    public ApiResponse<Void> updateUser(UserUpdatePO po) {
        if(getById(po.getEmpId()).getRoleId()==10){
            throw new BusException("Sorry,您不具备该权限");
        }
        User user = BeanUtil.beanA2beanB(po, User.class);
        updateById(user);
        return new ApiResponse("修改成功");
    }

    @Override
    public ApiResponse<UserVO> getAll(UserGetPO po) {
        int count = count();
        Page<User> pages = new Page<>(po.getPageNumber(), po.getPageSize());
        Page<User> userPages = page(pages,new LambdaQueryWrapper<User>()
                .eq(User::getIsDelete,0)
                .orderByDesc(User::getGmtCreate));
        if(userPages.getRecords().isEmpty()){
            return new ApiResponse<>(HttpStatusEnum.RE_CODE_NO_HAVE_OBJECT);
        }
        List<UserVO> userVOS = BeanUtil.listA2ListB(userPages.getRecords(), UserVO.class);
        for(UserVO userVO:userVOS){
            userVO.setGmdCreate(userVO.getGmtCreate().toLocalDate());
        }
        return new ApiResponse<>(userVOS,pages.getTotal(),count);
    }

    @Override
    public ApiResponse<UserVO> getByConditions(UserConditionPO po) {
        int count = count(new LambdaQueryWrapper<User>()
                .eq(po.getUsername()!=null, User::getUsername, po.getUsername())
                .eq(po.getStudentNumber()!=null, User::getStudentNumber, po.getStudentNumber())
                .eq(po.getRoleId()!=null , User::getRoleId, po.getRoleId())
                .eq(po.getGroupId()!=null, User::getGroupId, po.getGroupId())
                .eq(po.getSex()!=null, User::getSex, po.getSex())
                .eq(po.getStatus()!=null, User::getStatus, po.getStatus())
                .eq(po.getIsMatrix()!=null, User::getIsMatrix, po.getIsMatrix())
                .eq(po.getUserLevel()!=null, User::getUserLevel, po.getUserLevel())
                .eq(po.getMentorId()!=null, User::getMentorId, po.getMentorId())
                .eq(po.getIsSubscribe()!=null, User::getIsSubscribe, po.getIsSubscribe())
//                .eq(User::getIsDelete,0)
                .orderByDesc(User::getScore)
        );
        Page<User> pages = new Page<>(po.getPageNumber(),po.getPageSize());

        Page<User> userPages = page(pages,new LambdaQueryWrapper<User>()
                .eq(po.getUsername() != null, User::getUsername, po.getUsername())
                .eq(po.getStudentNumber() != null, User::getStudentNumber, po.getStudentNumber())
                .eq(po.getRoleId() != null && po.getUsername() == null && po.getStudentNumber() == null, User::getRoleId, po.getRoleId())
                .eq(po.getGroupId() != null && po.getUsername() == null && po.getStudentNumber() == null, User::getGroupId, po.getGroupId())
                .eq(po.getSex() != null && po.getUsername() == null && po.getStudentNumber() == null, User::getSex, po.getSex())
                .eq(po.getStatus() != null && po.getUsername() == null && po.getStudentNumber() == null, User::getStatus, po.getStatus())
                .eq(po.getIsMatrix() != null && po.getUsername() == null && po.getStudentNumber() == null, User::getIsMatrix, po.getIsMatrix())
                .eq(po.getUserLevel() != null && po.getUsername() == null && po.getStudentNumber() == null, User::getUserLevel, po.getUserLevel())
                .eq(po.getMentorId() != null && po.getUsername() == null && po.getStudentNumber() == null, User::getMentorId, po.getMentorId())
                .eq(po.getIsSubscribe() != null && po.getUsername() == null && po.getStudentNumber() == null, User::getIsSubscribe, po.getIsSubscribe())
                .orderByDesc(User::getScore));
        if(userPages.getRecords()==null){
            return new ApiResponse<>(HttpStatusEnum.RE_CODE_NO_HAVE_OBJECT);
        }
        List<UserVO> userVOS = BeanUtil.listA2ListB(userPages.getRecords(), UserVO.class);
        for (UserVO userVO : userVOS) {
            userVO.setGmdCreate(userVO.getGmtCreate().toLocalDate());
        }
        return new ApiResponse<>(userVOS,pages.getTotal(),count);
    }


}
